import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/order/ufMallOrder/list',
  listOffline = '/order/ufMallOrder/list/offline',
  listDetail = '/order/ufMallOrder/list/detail',
  listItemDetail = '/order/ufMallOrderItem/detail/list',
  save = '/order/ufMallOrder/add',
  edit = '/order/ufMallOrder/edit',
  deleteOne = '/order/ufMallOrder/delete',
  deleteBatch = '/order/ufMallOrder/deleteBatch',
  importExcel = '/order/ufMallOrder/importExcel',
  exportXls = '/order/ufMallOrder/exportXls',
  exportOfflineOrderXls = '/order/ufMallOrder/exportOfflineOrderXls',
  exportDetailXls = '/order/ufMallOrderItem/detail/export',
  ship = '/order/ufMallOrder/ship',
  refund = '/order/ufMallOrder/refund',
  exportBatchDeliveryTemplateXls = '/order/ufMallOrder/exportBatchDeliveryTemplateXls',
  importBatchDeliveryExcel = '/order/ufMallOrder/importBatchDeliveryExcel',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
export const getExportOfflineOrderUrl = Api.exportOfflineOrderXls;
export const getDetailExportUrl = Api.exportDetailXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 批量发货模板下载
 */
export const getExportBatchDeliveryTemplateXls = Api.exportBatchDeliveryTemplateXls;
export const getImportBatchDeliveryExcel = Api.importBatchDeliveryExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });
export const listDetail = (params) => defHttp.get({ url: Api.listDetail, params });
export const listItemDetail = (params) => defHttp.get({ url: Api.listItemDetail, params });
export const listOffline = (params) => defHttp.get({ url: Api.listOffline, params });
/**
 * 发快递
 * @param params
 */
export const shipOrder = (params) => defHttp.post({ url: Api.ship, params });

export const refundOrder = (params) => defHttp.post({ url: Api.refund, params });
/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp
        .delete(
          {
            url: Api.deleteBatch,
            data: params,
          },
          { joinParamsToUrl: true }
        )
        .then(() => {
          handleSuccess();
        });
    },
  });
};
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};
